"""
数据导入页面
"""
import streamlit as st
from data.loaders.file_loader import FileLoader
from data.loaders.db_loader import DBLoader
from data.loaders.manual_loader import ManualLoader


def show():
    """
    显示数据导入页面
    """
    st.title("数据导入")
    
    # 数据源类型选择
    data_source = st.radio(
        "选择数据源类型",
        options=["文件上传", "数据库连接", "手动输入"],
        horizontal=True
    )
    
    if data_source == "文件上传":
        st.subheader("文件上传")
        uploaded_file = st.file_uploader(
            "上传文件 (Excel/CSV/JSON)",
            type=["xlsx", "csv", "json"]
        )
        if uploaded_file is not None:
            loader = FileLoader()
            data = loader.load(uploaded_file)
            st.success(f"成功加载 {uploaded_file.name}")
            st.dataframe(data.head())
    
    elif data_source == "数据库连接":
        st.subheader("数据库连接")
        db_type = st.selectbox(
            "数据库类型",
            options=["MySQL", "PostgreSQL", "SQLite"]
        )
        host = st.text_input("主机地址")
        port = st.text_input("端口号")
        username = st.text_input("用户名")
        password = st.text_input("密码", type="password")
        database = st.text_input("数据库名称")
        
        if st.button("测试连接"):
            loader = DBLoader(db_type, host, port, username, password, database)
            if loader.test_connection():
                st.success("连接成功！")
            else:
                st.error("连接失败，请检查配置")
    
    else:
        st.subheader("手动输入")
        manual_data = st.text_area("输入数据 (JSON格式)")
        if st.button("加载数据"):
            loader = ManualLoader()
            data = loader.load(manual_data)
            st.success("数据加载成功")
            st.json(data)